﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using Convertor.Interface;
using Wpf.CommonTools.StaticExtension;
using Wpf.CommonTools.SystemExtension;

namespace Convertor
{
    /// <summary>
    /// AboutUs.xaml 的交互逻辑
    /// </summary>
    public partial class AboutUs : Window
    {
        private static bool updated = false;
        private PopForm p;
        public AboutUs()
        {
            InitializeComponent();
            p = new PopForm(this);
        }

        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            if (!updated)
            {
                if (!Config.GlobalConfig.HasUpdate)
                {
                    UpdateHandler u = new UpdateHandler();
                    u.HasUpdateEnvent += u_HasUpdateEnvent;
                    u.StartCheckUpdate();
                    Loading.Start();
                }
            }
        }

        private bool close;
        private void Window_KeyDown(object sender, KeyEventArgs e)
        {
            if (Config.GlobalConfig.EnableEscToCloseChildWindows && this.IsActive && e.Key == Key.Escape)
            {
                close = true;
            }
        }

        private void Window_KeyUp(object sender, KeyEventArgs e)
        {
            if (close && Config.GlobalConfig.EnableEscToCloseChildWindows && this.IsActive && e.Key == Key.Escape)
            {
                this.Close();
            }
        }

        void u_HasUpdateEnvent(object sender, UpdateEventArg e)
        {
            if (e.HasUpdate)
            {
                p.PopAlertMessage(UILanguage.FindResource("Help_Update_Url").ToString());
            }
            Loading.Stop();
        }

        private void Support_Click(object sender, RoutedEventArgs e)
        {
            System.Diagnostics.Process.Start(Config.HELP_URL);
        }

        private void Update_Click(object sender, RoutedEventArgs e)
        {
            System.Diagnostics.Process.Start(Config.DOWNLOAD_URL);
        }

        private void DeleteOrAddPlugin_Click(object sender, RoutedEventArgs e)
        {
            IPluginItem item = AllPluginList.SelectedItem as IPluginItem;
            if (item != null)
            {
                if (Config.GlobalConfig.DisabledPlugins.Contains(item.Path))
                {
                    Config.GlobalConfig.DisabledPlugins.Remove(item.Path);
                }
                else
                {
                    Config.GlobalConfig.DisabledPlugins.Add(item.Path);
                }
            }
        }

        private void Button_OK_Click(object sender, RoutedEventArgs e)
        {
            this.Close();
        }
    }
}
